package com.zhongke.cotrun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.mapper.IntegralRecordMapper;
import com.zhongke.cotrun.mapper.MembershipValuesMapper;
import com.zhongke.cotrun.mapper.UserInfoMapper;
import com.zhongke.cotrun.model.pojo.IntegralOfType;
import com.zhongke.cotrun.model.pojo.IntegralRecord;
import com.zhongke.cotrun.model.pojo.IntegralRecordVo;
import com.zhongke.cotrun.model.pojo.MembershipValues;
import com.zhongke.cotrun.service.IntegralOfTypeService;
import com.zhongke.cotrun.service.IntegralRecordService;
import com.zhongke.cotrun.service.UserIntegralHistoryService;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class IntegralRecordServiceImpl implements IntegralRecordService {

    @Autowired
    IntegralRecordMapper integralRecordMapper;//积分规则

    @Autowired
    UserInfoMapper userInfoMapper;//用户类


    @Autowired
    IntegralOfTypeService integralOfTypeService;//积分规则所属的二级类


    @Autowired
    MembershipValuesMapper membershipValuesMapper;//积分股则里面的具体值
    @Autowired
    UserIntegralHistoryService userIntegralHistoryService;//积分历史

    /**
     * 新增积分规则
     *
     * @param integralRecord
     * @return
     */
    @Override

    public Boolean insertIntegralRecord(IntegralRecord integralRecord) {

        if (integralRecordMapper.insertIntegralRecord(integralRecord)) {
            List<MembershipValues> m = integralRecord.getMembershipValues();
            for (MembershipValues s : m) {
                s.setOfIntegralRecordId(integralRecord.getIntegralRecordId());
            }

            return membershipValuesMapper.insertListMemberShipValues(m);
        }

        return false;
    }

    /**
     * 查看具体的积分规则
     *
     * @param id
     * @return
     */
    @Override
    public IntegralRecordVo selectIntegralRecordByid(Integer id) {
        IntegralRecordVo vo = integralRecordMapper.selectIntegralRecordById(id);

        if (vo == null) {
            return null;
        }
        return resortIntegralRecordVo(vo);
    }

    /**
     * 跟新
     *
     * @param integralRecord
     * @return
     */
    @Override
    public Boolean updateIntegralRecord(IntegralRecord integralRecord) {
        return integralRecordMapper.updateIntegralRecord(integralRecord);
    }

    /**
     * 分页查询积分规则（按条件检索）
     *
     * @param integralRecord
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo selectIntegralRecordByCondition(IntegralRecord integralRecord, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<IntegralRecordVo> l = integralRecordMapper.selectAllIntegralRecord(integralRecord);

        if (l == null || l.size() == 0) {
            return new PageInfo<>(l);
        }
        for (IntegralRecordVo integralRecordVo : l) {
            integralRecordVo.setLinkUserName(userInfoMapper.selectSysUserNameById(integralRecordVo.getLinkUserId()));
            resortIntegralRecordVo(integralRecordVo);

        }

        return new PageInfo<>(l);
    }

    /**
     * 根据类型id查看积分规则
     *
     * @param integralNameId
     * @return
     */
    @Override
    public IntegralRecordVo selectIntegralRecordByIntegralType(Integer integralNameId) {
        IntegralRecordVo integralRecordVo = integralRecordMapper.selectIntegralRecordByIntegralNameId(integralNameId);
        if (integralRecordVo == null) {
            return null;
        }
        return resortIntegralRecordVo(integralRecordVo);
    }


    /**
     * 返回规则(转换规则类型)工具封装
     */

    public IntegralRecordVo resortIntegralRecordVo(IntegralRecordVo vo) {
        IntegralOfType I = null;

        if (vo.getRankOne() != null) {
            I = integralOfTypeService.selectIntegralOfTypeByCode(vo.getRankOne());
        }


        if (I != null) {
            vo.setRankOneName(I.getOfTypeName());
        }

        if (vo.getRankTwo() != null) {
            I = integralOfTypeService.selectIntegralOfTypeByCode(vo.getRankTwo());
        }


        if (I != null) {
            vo.setRankTwoName(I.getOfTypeName());
        }

        return vo;
    }

    /**
     * 新建修改积分规则
     */
    @Transactional
    public Boolean insertUpdateIntegralRecord(IntegralRecord integralRecord) {
        boolean flage = false;
        if (integralRecord.getIntegralRecordId() == null) {

            //新增
            return insertIntegralRecord(integralRecord);
        } else {
            //   1.删除积分所属规则表里面的值
            membershipValuesMapper.deleteMembershipValuesByIntegralRecordId(integralRecord.getIntegralRecordId());
            flage = integralRecordMapper.updateIntegralRecord(integralRecord);

        }
        if (flage) {

            List<MembershipValues> m = integralRecord.getMembershipValues();
            for (MembershipValues s : m) {
                s.setOfIntegralRecordId(integralRecord.getIntegralRecordId());
            }

            return membershipValuesMapper.insertListMemberShipValues(m);


        }
        return false;

    }

    /**
     * 查看自己的权益全部的
     *
     * @param integralRecordVo
     * @return
     */
    @Override
    public List<IntegralRecordVo> selectRights(IntegralRecordVo integralRecordVo) {

        List<IntegralRecordVo> l = integralRecordMapper.selectRights(integralRecordVo);

        for (IntegralRecordVo i : l) {
            resortIntegralRecordVo(i);
        }

        return l;
    }

    /**
     * 根据身份取出最大的
     *
     * @param integralRecordVo
     * @param memberGrade      vip等级
     * @param w                身份
     * @return
     */

    public List<IntegralRecordVo> selectOfMyBig(IntegralRecordVo integralRecordVo, String memberGrade, WebUserEntity w) {


        List<IntegralRecordVo> mo = new ArrayList();

        integralRecordVo.setMemberGrade("全部");
        integralRecordVo.setTypeOfUser("全部");
        mo.addAll(selectRights(integralRecordVo));

        if (w.getIdentity() == null || "".equals(w.getIdentity())) {

            integralRecordVo.setMemberGrade("全部");
            if ("1".equals(w.getType())) {
                integralRecordVo.setTypeOfUser("普通用户");
            } else {
                integralRecordVo.setTypeOfUser("机构");
            }

            mo.addAll(selectRights(integralRecordVo));

            integralRecordVo.setMemberGrade(memberGrade);

            if ("1".equals(w.getType())) {
                integralRecordVo.setTypeOfUser("普通用户");
            } else {
                integralRecordVo.setTypeOfUser("机构");
            }

            mo.addAll(selectRights(integralRecordVo));

            integralRecordVo.setMemberGrade(memberGrade);
            integralRecordVo.setTypeOfUser("全部");
            mo.addAll(selectRights(integralRecordVo));
            //查询积分规则（自己拥有的权益）
        } else {
            integralRecordVo.setMemberGrade(memberGrade);
            integralRecordVo.setTypeOfUser("全部");
            List<IntegralRecordVo> k = selectRights(integralRecordVo);
            mo.addAll(k);
            String[] s = w.getIdentity().split(",");
            for (String a : s) {

                integralRecordVo.setTypeOfUser(a);
                List<IntegralRecordVo> k1 = selectRights(integralRecordVo);
                mo.addAll(k1);
                integralRecordVo.setMemberGrade("全部");
                integralRecordVo.setTypeOfUser(a);
                List<IntegralRecordVo> k2 = selectRights(integralRecordVo);
                mo.addAll(k2);

            }

        }

        if (mo.size() == 0) {
            return null;
        }

//根据身份取出最大的
        Map<Integer, IntegralRecordVo> map = new HashMap<>();
        for (IntegralRecordVo vo : mo) {
            if (map.containsKey(vo.getIntegralRecordId())) {
                List<MembershipValues> l1 = vo.getMembershipValues();
                IntegralRecordVo v = map.get(vo.getIntegralRecordId());

                List<MembershipValues> l2 = v.getMembershipValues();

                int re = (l1.get(0).getIntegralValue()).compareTo((l2.get(0).getIntegralValue()));


                if (re == 0 || re > 0) {
                    map.put(vo.getIntegralRecordId(), vo);

                }

            } else {
                map.put(vo.getIntegralRecordId(), vo);
            }


        }


        mo = null;


        List<IntegralRecordVo> vol = new ArrayList<IntegralRecordVo>(map.values());

        return vol;
    }


}